/*
	Copyright (c) 2008, Grant Jones <grant@podtronics.com>.
	All rights reserved.

	Redistribution and use in source and binary forms, with or
	without modification, are permitted provided that the following
	conditions are met:

		Redistributions of source code must retain the above copyright
		notice, this list of conditions and the following disclaimer.
		
		Redistributions in binary form must reproduce the above copyright
		notice, this list of conditions and the following disclaimer in
		the documentation and/or other materials provided with the
		distribution.
		
		Neither the name of the gxjones.com nor the names of its
		contributors may be used to endorse or promote products derived
		from this software without specific prior written permission.
	
	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
	LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
	FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
	SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
	OF THE POSSIBILITY OF SUCH DAMAGE.
	
	http://www.opensource.org/licenses/bsd-license.php
*/

/*
 *  radixtree_cache.c
 *  edb
 *
 *  Created by Grant Jones on 5/28/08.
 *
 */

#include "radixtree.h"
#include "radixtree_cache.h"

/* ************************************************************************
************************************************************************ */
/*!
	@function rt_cache_get_key
	@abstract locates the nearest tag location relating to key
	@discussion Similar to rt_locate_key but operates off the MRU rtree
		cache.
*/
off_t rt_cache_get_key( rtree_t *rt, rtvalue_t *key, size_t *last_step_loc, rtvalue_t *key_left, int *result )
{
	key_left->data = NULL;
	key_left->size = 0;
	
	*result = 0;	/* not found */
	*last_step_loc = 0;	/* last step location is the offset to last know good starting point */

	rtree_cache_cursor_t *cursor_cache = cmap_lookup( rt->key_cache, rt->list_cursor);
	if(!cursor_cache)
		return 0;
		
	rtree_cache_item_t *cache_item = strmap_lookup( cursor_cache->cache_items, key->data, key->size );
	if(cache_item)
	{
		*result = 1;
//		printf("using cache result : %ld\n", (long)cache_item->use_count );
		return cache_item->last_loc;
	}
	
	return 0;
}

/*!
	@function rt_cache_remove_lru
	@abstract removes the lru cache item
	@discussion 
*/
void rt_cache_remove_lru( rtree_cache_cursor_t *cursor_cache )
{
	strmapEnumStruct e;
	rtree_cache_item_t *cache_item;
	char *lru_key = NULL;
	size_t lru_key_sz = 0;
	char *key;
	size_t key_sz;
	size_t lowest_count = 0;

	/* TODO: fix dummy implementation */
	strmap_enum( cursor_cache->cache_items, &e );
	while( (cache_item = strmap_enum_next( &e, &key, &key_sz )) )
	{
		if( lru_key == NULL )
		{
			lowest_count = cache_item->use_count;
			lru_key = key;
			lru_key_sz = key_sz;
			continue;
		}
		
		if( lowest_count < cache_item->use_count )
			continue;

		lowest_count = cache_item->use_count;
		lru_key = key;
		lru_key_sz = key_sz;		
	}	

	if( lru_key )
	{
		strmap_remove(cursor_cache->cache_items, lru_key, lru_key_sz );
//		printf("lru remove: \"");
//		fwrite(lru_key, lru_key_sz, 1, stdout );
//		printf("\"\n");
	}
	
}

/*!
	@function rt_cache_put_key
	@abstract stores the result of a rt_locate_key operation
	@discussion 
*/
int rt_cache_put_key( rtree_t *rt, rtvalue_t *key, off_t last_loc )
{
	if( rt->max_cache_items == 0 )
		return 0;
	if( key->size > RTREE_MAX_CACHE_KEY_SZ )	/* don't cache very large keys */
		return 0;	
		
	rtree_cache_item_t *e = (rtree_cache_item_t *)mempool_malloc(rt->key_cache->mempool, sizeof(rtree_cache_item_t) );
	
	rtree_cache_cursor_t *cursor_cache = cmap_lookup( rt->key_cache, rt->list_cursor);
	
	if(!cursor_cache)
	{
		cursor_cache = cmap_assign_new(rt->key_cache, rt->list_cursor, sizeof(rtree_cache_cursor_t) );
		cursor_cache->cache_items = NULL;
		rt_cache_invalidate2( cursor_cache );
	}

	e->last_loc = last_loc;
	e->use_count = cursor_cache->next_cache_num ++;
	
	if( cursor_cache->cache_items->num > rt->max_cache_items )
		rt_cache_remove_lru( cursor_cache );
	
	strmap_assign( cursor_cache->cache_items, key->data, key->size, e );

	return 0;
}


int rt_cache_invalidate2( rtree_cache_cursor_t *cursor_cache )
{
	if( cursor_cache->cache_items )
	{
		if( cursor_cache->cache_items->num == 0 )	/* if cache was never used, just leave it */
			return 0;
		cmap_destroy( cursor_cache->cache_items );
	}

	cursor_cache->cache_items = cmap_new(NULL);
	cursor_cache->next_cache_num = 0;

	return 0;
}
/*!
	@function rt_cache_invalidate
	@abstract Invalidates key cache
	@discussion 
*/
int rt_cache_invalidate( rtree_t *rt )
{
	rtree_cache_cursor_t *cursor_cache = cmap_lookup( rt->key_cache, rt->list_cursor);
	if(!cursor_cache)
		return 0;
	return rt_cache_invalidate2( cursor_cache );
}

